﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Fin;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.Finance;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Finance
{
    public partial class frmMaintainCashFlowDetail : BaseForm.frmBaseDialogyBusinessForm
    {
        #region Constructor
        public frmMaintainCashFlowDetail()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbCurrency, "CS", true);
            UICommon.UICommonFunctions.BindComboboxByCommonItemWithCode(cmbCashFlowType, "CFT", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCURRENCY, "CS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItemWithCode(colCASH_FLOW_TYPE, "CFT", true);
        }
        private string GetCondition()
        {
            string condition = "";
            if (txtPerson.Tag != null && txtPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity person = (MstPersonEntity)txtPerson.Tag;
                condition += " AND CF.PERON_ID = '" + person.PersonId + "'";
            }
            if (string.IsNullOrEmpty(cmbCurrency.Text) == false && cmbCurrency.SelectedValue != null)
            {
                condition += " AND CF.CURRENCY = '" + cmbCurrency.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(cmbCashFlowType.Text) == false && cmbCashFlowType.SelectedValue != null)
            {
                condition += " AND CF.CASH_FLOW_TYPE = '" + cmbCashFlowType.SelectedValue.ToString() + "'";
            }
            if (dtpCashFlowDate.EditValue!=null)
            {
                condition += " AND CF.CASH_FLOW_DATE >= CONVERT(DATETIME,'" + dtpCashFlowDate.EditValue.ToString() + " 00:00:00')";
            }
            if (dtpCashFlowDateTo.EditValue != null)
            {
                condition += " AND CF.CASH_FLOW_DATE <= CONVERT(DATETIME,'" + dtpCashFlowDateTo.EditValue.ToString() + " 23:59:59')";
            }
            if (txtAccountsCode.Tag != null)
            {
                FinAccountsEntity account = (FinAccountsEntity)txtAccountsCode.Tag;
                condition += " AND CF.ACCOUNTS_CODE = '" + account.AccountsCode + "'";
            }
            return condition;
        }
        private void BindDataGrid_CashFlow()
        {
            string condition = GetCondition();
            string orderBy = "CF.CASH_FLOW_DATE DESC, CF.CURRENCY, CF.PERON_ID";
            DataTable dt = FinCashFlowBs.GetByCondition(condition, orderBy, "");
            gvCashFlow.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobox();
            BindDataGrid_CashFlow();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            BindDataGrid_CashFlow();
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPerson.Tag = frm.SelectedPerson;
                txtPerson.Text = frm.SelectedPerson.NameCn;
            }
            else
            {
                txtPerson.Tag = null;
                txtPerson.Text = "";
            }
        }

        private void txtAccountsCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainFinanceAccountsCodeQuery frm = new frmMaintainFinanceAccountsCodeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedAccount != null)
            {
                txtAccountsCode.Text = frm.SelectedAccount.AccountsName;
                txtAccountsCode.Tag = frm.SelectedAccount;
            }
            else
            {
                txtAccountsCode.Text = "";
                txtAccountsCode.Tag = null;
            }
        }

        #endregion
    }
}
